define(function(require, exports, module) {
	// 通过 require 引入依赖
	var $ = require("jQuery");
	var ajax = require("../common/util/ajax");
	require('../common/util/ajaxExt');
	require("../common/common.css");
	var Handlebars = require("handlebars");
	var helpers = require("../common/helpers");
	Handlebars.registerHelper(helpers);
	var Util = require("../common/util/util");
	require("../common/ellipsis/ellipsis");
	var moment = require("moment");
	require("./teacherQuestionBank.css");
	require("../common/common_require");

	var current = 1;
	var length = 10;
	var singleQuestionData;
	var index;

	$('input').on('ifChecked', function(e) {
		$("#q_list").bootstrapTable('refresh', {
			url : '/qaplus/q/query_t_question.json'
		});
	});
	$('input').on('ifUnchecked', function(e) {
		$("#q_list").bootstrapTable('refresh', {
			url : '/qaplus/q/query_t_question.json'
		});
	});
	$("#toAddHomework").click(function(){
		questionList.toAddHomework();
	});
	$("#toAddQuestion").click(function(){
		questionList.toAddOrUpdateQuestion('add');
	});
	
	$("#addOrUpdateButton").click(function(){
		if($(this).data("oper")=="add"){
			questionList.doAddTeacherQuestion();
		}else if($(this).data("oper")=="update"){
			questionList.doUpdateTeacherQuestion();
		}
	});
	
	$("#exportTQuestion").click(function(){
		questionList.exportTQuestion();
	});
	$("#importTQuestion").click(function(){
		questionList.importTQuestion();
	});

	var questionList = {
		initPage : function() {
			$('.question_search input').each(function() {
				var self = $(this);
				var label = self.next();
				var label_text = label.text();
				label.remove();
				self.iCheck({
					checkboxClass : 'icheckbox_sm-blue',
					radioClass : 'radio_sm-blue',
					insert : label_text
				});
			});
		},
		initTableData : function() {
			$('#q_list')
					.bootstrapTable(
							{
								url : "/qaplus/q/query_t_question.json",
								method : 'post',
								height : 450,
								cache : false,
								dataType : "json",
								contentType : "application/x-www-form-urlencoded",
								queryParamsType : "limit",
								cache : false, // 是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
								queryParams : questionList.queryParams,// 传递参数（*）
								detailView : true,
								search : true,
								formatSearch:function(){
									return "输入标题模糊搜索";
								},
								searchText:Util.getUrlParam("title"),
								detailFormatter : function(index, row) {
									var html = [];
									$.each(row, function(key, value, index) {
										if (key == 'title' || key == 'content'
												|| key == 'recCreateDate'
												|| key == 'category') {
											var val = "";
											if (key == 'title') {
												val = "问题标题";
											} else if (key == 'content') {
												val = "问题描述";
											} else if (key == 'recCreateDate') {
												val = "提问时间";
											} else if (key == 'category') {
												val = "问题分类";
											}

											html.push('<p><b>' + val + ':</b> '
													+ value + '</p>');
										}
									});
									return html.join('');
								},
								pagination : true,
								sidePagination : "server",
								paginationPreText : "上一页",
								paginationNextText : "下一页",
								pageSize : 10,
								pageList : [ 10, 25, 50 ],

								onClickRow : function(row, tr) {
									/*if(tr.hasClass("selected")){
										tr.removeClass("selected");
									}else{
										tr.addClass("selected");
									}*/
								},// 单击row事件
								responseHandler : questionList.responseHandler,
								striped : false,
								formatNoMatches : function() {
									return '无符合条件的记录';
								},
								onLoadSuccess : function(result) {
									$(".del").click(function(){
										questionList.del($(this).data("id"));
									});
									$(".update").click(function(){
										questionList.update($(this).data("id"));
									});
									$(".attach").click(function(){
										Util.downloadAttach($(this).data("id"));
									});
								},
								columns : [
										{
											field : 'state',
											width : '2%',
											checkbox : true
										},
										{
											field : 'i',
											width : '3%',
											title : '序号',
											formatter : function(value, row,
													index) {
												return index + 1;
											}
										},
										{
											field : 'title',
											title : '问题标题',
											width : "30%",
											class : "ellip"
										},
										{
											field : 'content',
											title : '问题描述',
											width : "30%",
											class : "ellip"
										},
										{
											field : 'category',
											width : "10%",
											title : '问题分类'
										},
										{
											field : 'recCreateDate',
											width : "10%",
											title : '创建时间',
											formatter : function(value, row,
													index) {
												return moment(value).format(
														"YYYY-MM-DD");
											}
										},
										{
											field : "questionHasAttachment",
											visible : false,
										},
										{
											title : '操作',
											field : 'id',
											width : "15%",
											formatter : function(value, row,
													index) {
												var f = "";
												var e = '<span class="label label-primary update" style="cursor:pointer;"  data-id='+row.id+'>编辑</span>&nbsp;';
												var d = '<span class="label label-danger del" style="cursor:pointer;"  data-id='+row.id+'>删除</span>&nbsp;'
												if (row.questionHasAttachment == "Y") {
													f = '<span class="label label-info attach" style="cursor:pointer;" data-id='+row.id+'>附件</span>&nbsp;	';
												}
												return f + e + d;
											}
										} ]
							});
		},
		queryParams : function(params) {
			$("#title").val(params.search);
			return $("#searchForm").serialize()+"&begin="+params.offset+"&end="+params.limit;
		},
		responseHandler : function(result) {
			if (result.hasError) {
				layer.msg(result.msg, {
					// 关闭后的操作
					icon : 5
				});
				return {
					"rows" : [],
					"total" : 0
				};
			} else {
				return {
					"rows" : result.data,
					"total" : result.count
				};
			}

		},
		exportTQuestion:function(){
			var searchText=$(".search input").val();
			$("#title").val(searchText);
			Util.downloadExcel("/qaplus/q/export_t_question.json",$("#searchForm"));
		},
		importTQuestion:function(){
			Util.uploadAttach($("#file-3"), null, {
				browseLabel : "上传Excel&hellip;",
				uploadUrl : "/qaplus/q/import_t_question.json",
				ajaxSettings:{
					success:function(result){
						window.location.reload(true);
					}
				}
			});
			
			layer.open({
				type : 1,//0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
//				area : [ '430px', '180px' ], 
				title : "Excel导入试题", 
				content : $('#importTQuestionDiv')
			});
		},
		queryQuestionById : function(id) {
			ajax({
				url : "/qaplus/q/query_tea_q.json",
				data : "id=" + id,
				success : function(result, status) {
					singleQuestionData = result;
				},
			});
		},
		del : function(id) {
			// 询问框
			layer.confirm('您确认删除这个问题？温馨提示：删除之后可在数据恢复区保存5天，5天之后将永久删除', {
				btn : [ '确认', '取消' ]
			// 按钮
			}, function(index) {
				ajax({
					url : "/qaplus/q/delete_tea_q.json",
					data : "id=" + id,
					success : function(result) {
						layer.close(index);
						window.location.reload(true);
					},
				});
			}, function(index) {
				layer.close(index);
			});
		},
		update : function(id) {
			ajax({
				url : "/qaplus/q/query_tea_q.json",
				data : "id=" + id,
				success : function(result) {
					$("#id").val(result.data[0].id);
					$("#titleUpdate").val(result.data[0].title);
					$("#content").text(result.data[0].content);
					$("#answer").text(result.data[0].answer);
					if (result.data[0].questionHasAttachment != undefined && result.data[0].questionHasAttachment == 'Y') {
						$("#oldQuestionAttachment").text( "原来问题附件：" + result.data[0].attachmentRealName);
					}
					questionList.toAddOrUpdateQuestion("update");
				},
			});
			ajax({
				url : "/qaplus/q/query_tea_a.json",
				data : "id=" + id,
				success : function(result) {
					if (result.data[0].answerHasAttachment != undefined
							&& result.data[0].answerHasAttachment == 'Y') {
						$("#oldAnswerAttachment").text("原来答案附件：" + result.data[0].attachmentRealName);
					}
				},
			});

		},
		toAddHomework : function() {
			var selectedRows = $('#q_list').bootstrapTable("getSelections");
			var ids = [];
			for (var i = 0; i < selectedRows.length; i++) {
				ids.push(selectedRows[i].id);
			}
			window.open("/qaplus/teaAddHomework/teaAddHomework.jsp?ids=" + ids,
					"布置家庭作业", null, null);
		},
		toAddOrUpdateQuestion : function(oper) {
			var titleContent="";
			if(oper=="add"){
				titleContent="添加问题到题库";
			}else if(oper=="update"){
				$("#addOrUpdateButton").text("确认修改");
				$("#addOrUpdateButton").data("oper","update");
				titleContent="修改问题";
			}
			//初始化select2  fileinput 
			$('#category2').select2({
				placeholder : "请至少选择一个问题分类",
				tags : true,
				width : '550px'
			});
			
			Util.uploadAttach($("#questionAttach"),$("#questionAttachmentId"),{browseLabel:"问题附件&hellip;"});
			Util.uploadAttach($("#answerAttach"),$("#answerAttachmentId"),{browseLabel:"答案附件&hellip;"});
			
			// 捕获页
			index = layer.open({
				type : 1,
				area : [ '600px', '550px' ], // 宽高
				shade : false,
				title : titleContent, // 不显示标题
				content : $('#addOrUpdateQuestionDiv')
			});
		},
		doAddTeacherQuestion : function() {
			ajax({
				url : "/qaplus/q/add_tea_q.json",
				data : $("#addOrUpdateTeacherQuestionForm").serialize(),
			});
		},
		doUpdateTeacherQuestion : function() {
			ajax({
				url : "/qaplus/q/update_tea_q.json",
				data : $("#addOrUpdateTeacherQuestionForm").serialize(),
			});
		}
	};
	// 输出接口，让boot.js启动
	module.exports = {
		init : function() {
			questionList.initPage();
			questionList.initTableData();
		}
	};
});
